package com.gitee.zycra.future.job.common.bo;

import com.gitee.zycra.future.job.common.dto.JobTriggerDTO;
import com.gitee.zycra.future.job.common.enums.ScheduleRatioEnum;
import com.gitee.zycra.jdbc.annotation.Column;
import com.gitee.zycra.jdbc.annotation.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * @author zycra
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
@Table
public class JobTriggerBO {

    /**
     * 执行ID
     */
    @Column("id")
    private Long runningId;

    /**
     * 任务调度ID
     */
    @Column("job_schedule_id")
    private Long jobScheduleId;

    /**
     * 执行参数
     */
    @Column("schedule_param")
    private String scheduleParam;

    /**
     * 下次调度时间
     */
    @Column("schedule_time")
    private LocalDateTime scheduleTime;

    /**
     * 任务类名
     */
    @Column("job_class_name")
    private String jobClassName;

    /**
     * 应用名称
     */
    @Column("application_name")
    private String applicationName;

    /**
     * 节点IP
     */
    @Column("node_ip")
    private String nodeIp;

    /**
     * 任务进度
     *
     * @see com.gitee.zycra.future.job.common.enums.ScheduleRatioEnum
     */
    @Column("schedule_ratio")
    private Integer scheduleRatio;

    public static JobTriggerDTO convertToJobTriggerDTO(JobTriggerBO jobTriggerBO) {
        if (jobTriggerBO == null) {
            return null;
        }
        return new JobTriggerDTO()
                .setRunningId(jobTriggerBO.getRunningId())
                .setScheduleParam(jobTriggerBO.getScheduleParam())
                .setScheduleTime(jobTriggerBO.getScheduleTime())
                .setJobClassName(jobTriggerBO.getJobClassName())
                .setCancel(ScheduleRatioEnum.CANCELED.getCode().equals(jobTriggerBO.getScheduleRatio()))
                .setTimeout(ScheduleRatioEnum.TIMEOUT.getCode().equals(jobTriggerBO.getScheduleRatio()));
    }
}
